package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsCategory;
import com.qf.pojo.DtsGoods;
import com.qf.service.CategoryService;
import com.qf.service.GoodsService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品类目
 * @author mkr
 * @version V1.0
 * @Project java2203-mkr
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/6/21 16:25
 */
@RestController
@RequestMapping("/category")
@Api(value = "")
//解决前后端跨域访问问题
@CrossOrigin("*")
public class CategoryController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 商品类目列表
     * @param goods_sn  根据商品编号查询
     * @param name      根据商品名字查询
     * @param page      当前页，默认1
     * @param limit     每页显示多少条，默认10条
     * @param sort      排序字段名，默认按照add_time添加时间排序
     * @param order     排序方式，默认desc降序
     * @return
     */
    @GetMapping("/list")
    public Object list(String goods_sn , String name ,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "20") Integer limit,
                       @RequestParam(defaultValue = "add_time") String sort,
                       @RequestParam(defaultValue = "desc") String order){

        //调用service业务方法
        IPage<DtsGoods> resultPage = goodsService.selectGoodsPage(goods_sn, name, page, limit, sort, order);

        //获取查询的数据
        List<DtsGoods> list = resultPage.getRecords();
        //获取数据总条数
        long total = resultPage.getTotal();

        //封装数据并返回数据
        Map<String , Object> data = new HashMap<>();
        data.put("total",total);
        data.put("items",list);

        return ResponseUtil.ok(data);

    }

    @GetMapping("/l1")
    public Object li(  ){

        //获取一级目录
        List<DtsCategory> L1list = categoryService.selectCategorylist();

        List l1ListVo = L1ListVo(L1list);

        Map <String , Object> data = new HashMap<>();
        data.put("volue", l1ListVo);

        return ResponseUtil.ok(data);

    }

    public List L1ListVo(List<DtsCategory> L1list){
        List<CatVo> catVos = new ArrayList<>();

        for ( DtsCategory category : L1list ){
            CatVo vo = new CatVo();
            vo.setValue(category.getId());
            vo.setLabel(category.getName());

            catVos.add(vo);
        }

        return catVos;
    }

    /**
     *添加
     * @param
     * @return
     */
    @PostMapping("/create")
    public Object Add(@RequestBody DtsCategory category){
        int count = categoryService.add(category);
        if (count == 0) {
            return ResponseUtil.fail();
        }
        return ResponseUtil.ok(category);
    }

    /**
     * 修改品牌信息
     * @return
     */
    @PostMapping("/update")
    public Object update(@RequestBody DtsCategory category){
        int count = categoryService.update(category);
        if (count == 0) {
            return ResponseUtil.fail();
        }
        return ResponseUtil.ok(category);
    }

    /**
     * 删除品牌信息
     * @return
     */
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsCategory category){
        int count = categoryService.delete(category);
        if (count == 0) {
            return ResponseUtil.fail();
        }
        return ResponseUtil.ok(category);
    }


}
